{
  // 声明函数
  function createArray(count: number, value: any): any[] {
    //声明空数组
    let arr: any[] = [];
    //循环
    for (let i = 0; i < count; i++) {
      arr.push(value);
    }
    //返回数组
    return arr;
  }

  //调用函数
  let result = createArray(3, "abc");

  // result[0].//这里没有提示
  // result[0].toFixed(2);//没有错误提醒
}

{
  // 泛型函数
  // 声明函数    T 泛型参数, 一般大写, 常用的泛型参数有 T V K R
  function createArray<T>(count: number, value: T): T[] {
    //声明空数组
    let arr: T[] = [];
    //循环
    for (let i = 0; i < count; i++) {
      arr.push(value);
    }
    //返回数组
    return arr;
  }

  //调用函数
  // let result = createArray<string>(3, 'love');
  let result = createArray<string>(3, "123");
  //操作元素
  // result[0].split('-')//有代码提示
  // result[0].toFixed(2);// 这里有报错提醒
}

{
  // 多个泛型参数  a,b  => [a, b]
  function createArray<T, K>(a: T, b: K): [T, K] {
    return [a, b];
  }

  //调用函数, 传入泛型参数的类型
  // let result = createArray<string, number>('a', 10);
  //泛型参数可以省略
  let result = createArray("aaa", 10);

  // result[0].  //代码提示
  // result[1].slice();//报错提醒
}

{
  interface Book {
    id: number;
    name: string;
    price: number;
  }

  interface Movie {
    id: number;
    title: string;
    director: string;
  }

  interface Response<T> {
    status: number;
    headers: object;
    data: T;
  }

  //第一个响应对象
  let response: Response<Book> = {
    status: 200,
    headers: {},
    data: {
      id: 1,
      name: "西游记",
      price: 28,
    },
  };

  //第二个响应对象
  let response2: Response<Movie> = {
    status: 200,
    headers: {},
    data: {
      id: 20,
      title: "大话西游",
      director: "周星驰",
    },
  };
}

{
  // 商品
  interface Goods {
    id: number;
    title: string;
    price: number;
  }

  interface Novel {
    id: number;
    name: string;
    author: string;
  }

  interface User {
    id: number;
    username: string;
    age: number;
  }

  //声明一个接口  接口中属性之间的分隔符可以使用『,』也可以使用『;』
  interface Response<T> {
    status: number;
    statusText: string;
    headers: object;
    data: T;
  }

  //有一个对象
  let response: Response<Goods> = {
    status: 200,
    statusText: "OK",
    headers: {},
    data: {
      id: 1,
      title: "9号电动车 - N70C",
      price: 2999,
    },
  };

  let response2: Response<Novel> = {
    status: 200,
    statusText: "OK",
    headers: {},
    data: {
      id: 20,
      name: "幻城",
      author: "小四",
    },
  };

  let response3: Response<User> = {
    status: 200,
    statusText: "OK",
    headers: {},
    data: {
      id: 20,
      username: "zhangsan",
      age: 28,
    },
  };
}


{

  interface Response<K> {
    status: number,
    statusText: string,
    headers: object,
    data: K
  }

  interface ResponseBody<T> {
    code: number,
    msg: string,
    data: T
  }

  interface Goods {
    id: number,
    title: string,
    price: number,
  }






  //有一个对象   0000   0001   0010    0020   0201
  let response: Response<ResponseBody<Goods>> = {
    status: 200,
    statusText: "OK",
    headers: {},
    data: {
      code: 2000,
      msg: '请求成功',
      data: {
        id: 1,
        title: "9号电动车 - N70C",
        price: 2999,
      }
    },
  };

  /*   interface Response2 {
      status: number;
      statusText: string;
      headers: {};
      data: ResponseBody2;
    } */

  /*   interface ResponseBody2 {
      code: number;
      msg: string;
      data: Song;
    }
   */
  interface Song {
    id: number;
    name: string;
    singer: string;
  }

  let response2: Response<ResponseBody<Song>> = {
    status: 200,
    statusText: "OK",
    headers: {},
    data: {
      code: 2000,
      msg: '请求成功',
      data: {
        id: 20,
        name: '旅行',
        singer: '许巍'
      }
    },
  };
}



{

  interface Length{
    length:number
  }

  function getLength(arr:number[]){
    return arr.length
  }
  getLength([1,2,3,4])
}


